package data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fileProcessing.ParsingException;

public class ZipCodeEntry extends AbstractDataType<ZipCodeEntry>{

private String zipCode;
private String fieldType = "Zip Code";
	
public ZipCodeEntry(){};	

public ZipCodeEntry(RandomAccessFile raf) throws ParsingException{
		try {
			zipCode = raf.readUTF();
		} catch (IOException e) {
			throw new ParsingException("Error: "+e.getMessage()+" when reading date from table's data file");
		}
	}
	
	public ZipCodeEntry(String newZip) throws ParsingException{
		if(isValid(newZip)){
			zipCode = newZip;
		}
		else
			throw new ParsingException("Phone number not valid.");
	}	

	public boolean isValid(String val){
		Pattern pattern = Pattern.compile("\\d{5}(-\\d{4})?");
		return pattern.matcher(val).matches();
	}
	
	/*
	 * provide methods for referencing additional information from zip code.
	 * (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	
	@Override
	public int compareTo(ZipCodeEntry o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType() {
		return fieldType;
	}

}
